package com.boco.treatment.controller;



import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.boco.sp.external.model.Organization;
import com.boco.sp.external.model.Staff;
import com.boco.sp.external.model.SysUser;
import com.boco.sp.external.service.IUserService;

import java.security.Principal;
import java.util.List;

/**
 * @author sunyu.
 * 转到首页视图
 */
@Controller
public class IndexController {
	
	@Resource
	private IUserService iUserService;
  
    @RequestMapping(value = "/")
    public String index(HttpServletRequest request){
        setSession(request);
    	
        return "/index";
    }

    /**
     * 在session中获取用于名
     * @param request
     */
    private void setSession(HttpServletRequest request){
        Principal principal = request.getUserPrincipal();
        String userName = principal.getName();
        System.out.println("userName:"+userName);
        HttpSession session = request.getSession(); 
        List<SysUser> userList = iUserService.getUserInfoByUserName(userName);
        int userId = 0;
        if(userList != null && userList.size() != 0){
        	userId = userList.get(0).getId();
        	session.setAttribute("name",userList.get(0).getRealName());
        }
        System.out.println("userId:"+userId);
        
        //查询职工信息
        Staff staff = iUserService.getStaffInfoByUserId(userId);
        if(staff != null){
        	session.setAttribute("position", staff.getPosition());
        }
        //查询机构信息
        Organization org = iUserService.getOrgByUserId(userId);
        if(org != null){
        	session.setAttribute("orgName", org.getOrganizationName());
        }
        
        
    }
}
